from linecache import cache

from django.conf import settings
from django.db.models.expressions import result
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View

from goods.models import Catalog, SKU, SPU, SKUImage, SPUSaleAttr, SKUSpecValue
from utils.baseview import BaseView
from utils.cache_che import cache_check


# Create your views here.

class GoodsIndexView(View):
    def get(self, request):
        '''
        {
    "code": 200,
    "data": [
        {
            "catalog_id": 1,
            "catalog_name": "手提包",
            "sku": [
                {
                    "skuid": 1,
                    "caption": "测试1",
                    "name": "测试1234",
                    "price": "2.00",
                    "image": "1.jpg"
                },
                           ]
        }
    ],
    "base_url": "http://127.0.0.1:8000/media/"
}
        '''
        print("---mysql--")
        data = []
        all_catalog = Catalog.objects.all()
        for catalog in all_catalog:
            catalog_dict = {
                "catalog_id": catalog.id,
                "catalog_name": catalog.name,
                "sku": []
            }
            # spu_list=SPU.objects.filter(catalog=catalog).values("id")
            spu_list = catalog.spu_set.all().values("id")
            sku_list = SKU.objects.filter(spu__in=spu_list)
            sku_data = []
            for sku in sku_list:
                single_dict = {
                    "skuid": sku.id,
                    "caption": sku.caption,
                    "name": sku.name,
                    "price": sku.price,
                    "image": str(sku.default_image_url)
                }
                sku_data.append(single_dict)

                catalog_dict["sku"].append(single_dict)
            data.append(catalog_dict)
        print(data)
        result = {
            "code": 200,
            "data": data,
            "base_url": settings.PIC_URL
        }
        print(result)
        return JsonResponse(result)


class GoodsDetailView(View):
    '''
    #响应示例
# {"code":200, "data":{}, "base_url":xxx}
{
    "code": 200,
    "data": {
        # 类1:类别id 类别name
        "catalog_id": 1,
        "catalog_name": "手提包",

        # 类2：SKU
        "name": "手提包",
        "caption": "最时尚的手提包",
        "price": "12345.00",
        "image": "1.png",
        "spu": 2,

        # 类3：详情图片
        "detail_image": "v2-1.jpg",

        # 类4：销售属性
        "sku_sale_attr_id": [7,8]
        "sku_sale_attr_names": ["尺寸","颜色"],

        # 类5：销售属性值
        "sku_sale_attr_val_id": [11,12,13]
        "sku_sale_attr_val_names": ["18寸","19寸","蓝色"],

        # 销售属性和销售属性值的对应关系
        "sku_all_sale_attr_vals_id": {
            "7": [11,12],
            "8": [13]
        },
        "sku_all_sale_attr_vals_name": {
            "7": ["18寸","19寸"],
            "8": ["蓝色"]
        },

        # 类6和类7：规格属性名和规格属性值
        "spec": {
            "批次": "2000",
            "数量": "2000",
            "年份": "2000"
        }
    },
    "base_url": "http://127.0.0.1:8000/media/"
}
    '''
    @cache_check(key_prefix= "gd" , key_param="sku_id",cache="goods_detail")
    def get(self, request, sku_id):
        print("---mysql--")
        data = {}
        sku_item = SKU.objects.get(id=sku_id)
        data["catalog_id"] = sku_item.spu.catalog.id
        data["catalog_name"] = sku_item.spu.catalog.name

        data["name"] = sku_item.name
        data["caption"] = sku_item.caption
        data["price"] = sku_item.price
        data["image"] = str(sku_item.default_image_url)
        data["spu"] = sku_item.spu.id

        data["detail_image"] = str(SKUImage.objects.filter(sku=sku_item))

        sale_attr_value_query = sku_item.sale_attr_value.all()

        # 销售属值id和销售属性值名
        if sale_attr_value_query:
            data["sku_sale_attr_val_id"], data["sku_sale_attr_val_names"] = zip(
                *[(attr_item.id, attr_item.name) for attr_item in sale_attr_value_query])
        else:
            data["sku_sale_attr_val_id"] = []
            data["sku_sale_attr_val_names"] = []
        # 销售属性id和销售属性名
        sale_attr_query = SPUSaleAttr.objects.filter(spu=sku_item.spu)
        if sale_attr_query:
            data["sku_sale_attr_id"], data["sku_sale_attr_names"] = zip(
                *[(attr_item.id, attr_item.name) for attr_item in sale_attr_query])
        sku_all_sale_attr_vals_id = {}
        sku_all_sale_attr_vals_name = {}

        for attr_item in sale_attr_query:

            sku_all_sale_attr_vals_id[attr_item.id] = []
            sku_all_sale_attr_vals_name[attr_item.id] = []

            for attr_value_item in sale_attr_value_query:

                sku_all_sale_attr_vals_id[attr_item.id].append(attr_value_item.id)
                sku_all_sale_attr_vals_name[attr_item.id].append(attr_value_item.name)

        data["sku_all_sale_attr_vals_id"] = sku_all_sale_attr_vals_id
        data["sku_all_sale_attr_vals_name"] = sku_all_sale_attr_vals_name

        spec={}
        spec_query = SKUSpecValue.objects.filter(sku=sku_item)
        for spec_item in spec_query:
            spec[spec_item.spu_spec.name]=spec_item.name


        result = {
                "code": 200,
                "data": data,
                "base_url": settings.PIC_URL
            }
        return JsonResponse(result)

    pass
